package com.likpia.greendaotest;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;

//import com.dzaitsev.android.widget.RadarChartView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import lecho.lib.hellocharts.model.Axis;
import lecho.lib.hellocharts.model.AxisValue;
import lecho.lib.hellocharts.model.Column;
import lecho.lib.hellocharts.model.ColumnChartData;
import lecho.lib.hellocharts.model.PieChartData;
import lecho.lib.hellocharts.model.SliceValue;
import lecho.lib.hellocharts.model.SubcolumnValue;
import lecho.lib.hellocharts.model.Viewport;
import lecho.lib.hellocharts.view.ColumnChartView;
import lecho.lib.hellocharts.view.PieChartView;
import rorbin.q.radarview.RadarData;

public class Fragment extends android.support.v4.app.Fragment implements RadioGroup.OnCheckedChangeListener {

    private MyRadarView radarView;
    private PieChartView pieChartView;
    private ViewGroup container;
    private ColumnChartView columnChartView;
    private RadioGroup radioGroup;
    private ColumnChartData columnChartData;

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        radarView = view.findViewById(R.id.radar_chart);
        container = view.findViewById(R.id.label_container);
        List<Float> values = new ArrayList<>();
        Collections.addAll(values, 28f, 47f, 33f, 38f, 40f);
        RadarData data = new RadarData(values);
        data.setColor(0x22FF7B39);
        radarView.addData(data);
        radarView.setMaxValue(60);
        radarView.addData(data);
        radarView.setVertexText(getTags());
        radarView.setVertexTextOffset(20);
        radarView.setLayerColor(getLayerColor());
        radarView.setRotationEnable(false);
        pieChartView = view.findViewById(R.id.pie_chart);
        List<SliceValue> sliceValues = getSliceValues();
        bindLabel(sliceValues);

        PieChartData chartData = new PieChartData();
        chartData.setValues(sliceValues).setHasCenterCircle(true).setCenterCircleScale(0.5f).setHasLabels(true);
        chartData.setValueLabelsTextColor(Color.WHITE);
        chartData.setValueLabelTextSize(10);
        chartData.setValueLabelBackgroundAuto(false);
        chartData.setValueLabelBackgroundColor(Color.TRANSPARENT);

        pieChartView.setPieChartData(chartData);

        radioGroup = view.findViewById(R.id.rg);
        radioGroup.setOnCheckedChangeListener(this);
        columnChartView = view.findViewById(R.id.column_char_view);
        columnChartView.setZoomEnabled(false);
        columnChartData = getRandomColumnData();

        columnChartData.setValueLabelBackgroundAuto(true);
        columnChartData.setValueLabelBackgroundEnabled(false);

        columnChartData.setValueLabelsTextColor(Color.BLACK);

        columnChartView.setColumnChartData(columnChartData);


    }

    private void prepareDataAnimation(List<Column> newData) {
        List<Column> columns = columnChartData.getColumns();
        for (int i = 0; i < columns.size(); i++) {
            List<SubcolumnValue> subcolumnValues = columns.get(i).getValues();
            for (int j = 0; j < subcolumnValues.size(); j++) {
                float val = newData.get(i).getValues().get(j).getValue();
                subcolumnValues.get(j).setTarget(val);
            }
        }
    }

    private Column getColumn(String label, float value, int color) {
        Column column = new Column();
        column.setHasLabels(true);
        List<SubcolumnValue> subcolumnValues = new ArrayList<>();

        SubcolumnValue subcolumnValue = new SubcolumnValue();
        subcolumnValue.setColor(color);

        subcolumnValue.setValue(value);
        subcolumnValue.setLabel(label);
        subcolumnValues.add(subcolumnValue);
        column.setValues(subcolumnValues);
        return column;
    }

    private List<Column> randomColumn() {
        List<Column> list = new ArrayList<>();
        list.add(getColumn("设备房间数", (float) (Math.random() * 300), randomColor()));
        list.add(getColumn("设备总数量", (float) (Math.random() * 300), randomColor()));
        list.add(getColumn("设备故障数", (float) (Math.random() * 300), randomColor()));
        list.add(getColumn("保修工单数", (float) (Math.random() * 300), randomColor()));
        return list;

    }


    private void bindLabel(List<SliceValue> sliceValues) {
        for (SliceValue v : sliceValues) {
            container.addView(ViewGenerateUtils.generatePieLabelView(getContext(), v.getColor(), String.valueOf(v.getLabelAsChars()), v.getValue()));

        }
    }

    private SliceValue getSliceValue(String label, int value, int color) {
        SliceValue sliceValue = new SliceValue();
        sliceValue.setColor(color);
        sliceValue.setValue(value);
        sliceValue.setLabel(label);
        return sliceValue;
    }

    private int randomColor() {
        return Color.rgb((int) (Math.random() * 360), (int) (Math.random() * 360), (int) (Math.random() * 360));
    }

    private List<SliceValue> getSliceValues() {
        List<SliceValue> sliceValues = new ArrayList<>();
        sliceValues.add(getSliceValue("空调", 56, randomColor()));
        sliceValues.add(getSliceValue("风机", 53, randomColor()));
        sliceValues.add(getSliceValue("户外端子箱", 110, randomColor()));
        sliceValues.add(getSliceValue("采集器", 115, randomColor()));
        sliceValues.add(getSliceValue("控制器", 110, randomColor()));
        sliceValues.add(getSliceValue("传感器", 100, randomColor()));
        sliceValues.add(getSliceValue("其他", 40, randomColor()));

        return sliceValues;
    }

    private List<String> getTags() {
        List<String> tags = new ArrayList<>();
        Collections.addAll(tags, "舒适度", "节能率", "故障率", "保修率", "巡检完成率");
        return tags;
    }

    private Axis getBottomAxis(List<Column> list) {
        Axis axis = new Axis();
        axis.setName("设备数");
        axis.setAutoGenerated(false);

//        axis.setAutoGenerated(false);
        List<AxisValue> axisValues = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            Column c = list.get(i);
            AxisValue axisValue = new AxisValue(i);
            axisValue.setLabel(String.valueOf(c.getValues().get(0).getLabelAsChars()));
            axisValues.add(axisValue);
        }
        axis.setValues(axisValues);
        return axis;
    }

    private List<Integer> getLayerColor() {
        List<Integer> integers = new ArrayList<>();
        integers.add(Color.WHITE);
        integers.add(0xFFEAEAEA);
        integers.add(Color.WHITE);
        integers.add(0xFFEAEAEA);
        integers.add(Color.WHITE);
        return integers;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_watch_board, container, false);
    }

    private ColumnChartData getRandomColumnData() {
        ColumnChartData columnChartData = new ColumnChartData();
        columnChartData.setValueLabelBackgroundAuto(false);
        List<Column> columns = randomColumn();
        columnChartData.setColumns(columns);
        columnChartData.setAxisXBottom(getBottomAxis(columns));

        return columnChartData;

    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {

        prepareDataAnimation(randomColumn());
        columnChartView.startDataAnimation();
//        switch (checkedId) {
//            case R.id.rb_together:
//
//                break;
//            case R.id.rb_7_day:
//                break;
//            case R.id.rb_30_day:
//                break;
//        }
    }
}
